/*
 ** time-pick 时间滚动选择组件
 **
 */
(function() {
    'use strict';

    angular
        .module('elife')
        .directive('timePicker', timePicker);
    timePicker.$inject = ['$filter', '$rootScope'];

    function timePicker($filter, $rootScope) {
        var directive = {
            restrict: 'A',
            require: '?ngModel',
            link: linkFunc
        };

        return directive;

        function linkFunc(scope, element, attr) {
            var rootScope = $rootScope;
            // rootScope.$on('$stateChangeStart', function() {
            //     return;
            // });
            var calendar = new lCalendar();

            var maxDate = attr.maxdate;
            var minDate = attr.mindate;
            var timetype = attr.timetype;

            var now = new Date();
            now = $filter('date')(now, 'yyyy-MM-dd');
            now = new Date(now);
            if (maxDate) {
                var maxtime = $filter('date')(scope[maxDate.split('.')[0]][maxDate.split('.')[1]], 'yyyy-MM-dd');
                var maxChooseTime = new Date(maxtime);
                if (maxChooseTime.getTime() < now.getTime()) {
                    now = maxChooseTime;
                }
            }
            if (minDate) {
                var mintime = $filter('date')(scope[minDate.split('.')[0]][minDate.split('.')[1]], 'yyyy-MM-dd');
            }


            calendar.init({
                'trigger': element[0],
                'type': timetype,
                'max': maxtime,
                'min': mintime,
                'chooseTime': now
            });
            // 页面跳转事件监听
            var rootScope = $rootScope;
            rootScope.$on('$stateChangeStart', function(event, toState) {
                if (calendar.gearDate) {
                    calendar.gearDate.remove('.gearDate');
                }
            });
        }
    }
})();